package edu.citadel.presentation.action;

import edu.citadel.db.ClientDao;
import edu.citadel.domain.Client;
import edu.citadel.presentation.resolution.GsonResolution;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;

import java.util.HashMap;
import java.util.Map;

/**
 * User: jordan
 * Date: 3/18/13
 * Time: 10:04 AM
 */
@UrlBinding("/ClientManager.action")
public class ClientManagerAction extends AbstractAction {

    @SpringBean
    private ClientDao clientDao;

    @Validate(on = "deleteClient", required = true, minlength = 1, maxlength = 255)
    private int contactId;

    private Client client;

    @DefaultHandler
    public Resolution display() {
        getLogger().info("IN CLIENT MANAGER!");
            return new ForwardResolution(getContext().getJspPath("client/dashboard.jsp"));

    }

    public Resolution editClient() {
       getLogger().info("EDIT CLIENT CALLED:  Saving client: " + client.getFirstName() + " " + client.getLastName() + " contact ID: "
               + client.getContactId() + " clientId: " + client.getClientId());
       client.save();
       return new RedirectResolution(DashboardAction.class);
    }


    public Resolution deleteClient() {

        getLogger().info("Deleting client with contact Id: " + contactId);
        client = clientDao.getClientByContactId(contactId);
        clientDao.deleteClient(client);

        if (getContext().isAjaxRequest()) {
            Map<String, String> jsonResponse = new HashMap<String, String>(1);
            jsonResponse.put("contactId", String.valueOf(contactId));
            return  new GsonResolution(jsonResponse);
        } else {
            return new RedirectResolution(DashboardAction.class);
        }
    }

    public ClientDao getClientDao() {
        getLogger().info("Get Client Dao Called!");
        return clientDao;
    }

    public void setClientDao(ClientDao clientDao) {
        getLogger().info("Set client dao called!");
        this.clientDao = clientDao;
    }

    public int getContactId() {
        return contactId;
    }

    public void setContactId(int contactId) {
        this.contactId = contactId;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }
}
